const UserModel = require('../../models/userModel');

const UserService = {
    //解构出用户名密码
    // 登录校验
    login: async ({ username, password }) => {
        return UserModel.find({
            //联通数据库对比用户信息
            username,
            password,
        });
    },
    // 更新用户数据
    upload: async ({ _id, username, introduction, gender, avatar }) => {
        if (avatar) {
            // 判断是否存在头像数据 如果有返回 没有则跳过头像信息
            return UserModel.updateOne(
                {
                    _id,
                },
                {
                    username,
                    introduction,
                    gender,
                    avatar,
                }
            );
        } else {
            return UserModel.updateOne(
                {
                    _id,
                },
                {
                    username,
                    introduction,
                    gender,
                }
            );
        }
    },
    //创建用户信息
    add: async ({ username, introduction, gender, role, password, avatar }) => {
        return UserModel.create({
            username,
            introduction,
            gender,
            role,
            password,
            avatar,
        });
    },
    //获取用户列表
    getList: async ({ id }) => {
        return id
            ? //有id走这套
              UserModel.findOne({ _id: id }, ['username', 'role', 'password', 'gender', 'introduction'])
            : //没有id走这套
              UserModel.find({}, ['username', 'role', 'avatar', 'gender', 'introduction']);
    },
    //删除用户
    delList: async ({ _id }) => {
        return UserModel.deleteOne({ _id });
    },
    //提交单条用户信息
    putList: async (id, body) => {
        return UserModel.updateOne({ _id: id }, body);
    },
};

module.exports = UserService;
